package com.micangcaihang.micang.mvp.task.presenter;

import com.micangcaihang.common.baserx.RxSchedulers;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.RxHelper;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.entity.FTaskCostDetailEntity;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.mvp.task.contract.FTaskCostDetailContract;
import com.micangcaihang.micang.mvp.task.model.FTaskCostDetailModel;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/17
 *     desc   : 风控-费用明细
 *     version: 1.0
 * </pre>
 */

public class FTaskCostDetailPresenter extends FTaskCostDetailContract.Presenter {
    private String id;
    private FTaskCostDetailModel mModel;
    private boolean isRefresh;

    @Override
    public void setV(FTaskCostDetailContract.View v) {
        super.setV(v);
        mModel = new FTaskCostDetailModel();
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        loadCostDetailData();
    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }

    /**
     * 加载下拉框数据
     *
     * @param type
     * @param title
     */
    @Override
    public void loadPickerViewData(String type, String title) {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getPickerViewData(type);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnPickerViewData(listApiResult.data, title);
                } else {
                    mView.showTip("暂时没有" + title + "信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 加载费用明细数据
     */
    @Override
    public void loadCostDetailData() {
        Observable<ApiResult<FTaskCostDetailEntity>> observable = mModel.getCostDetailData(id);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<FTaskCostDetailEntity>>() {

            @Override
            protected void doOnStart() {
                mView.showLoading();
            }

            @Override
            protected void doOnNext(ApiResult<FTaskCostDetailEntity> result) {
                if (result.data != null) {
                    mView.returnCostDetailData(result.data);
                }
            }

            @Override
            protected void doOnComplete() {
                mView.showContent();
            }

            @Override
            protected void doOnError(ApiException e) {
//                super.doOnError(e);
                mView.showNetError(e);
            }
        }));
    }


    /**
     * 保存提交保存信息
     *
     * @param request
     */
    @Override
    public void saveCostDetail(FTaskCostDetailEntity request) {
        Observable<ApiResult<FTaskCostDetailEntity>> costDetailData = mModel.getCostDetailData(id);
        Observable<ApiResult<Void>> observable = mModel.savaLoanerData(request);
        mRxManage.add(observable.concatMap(new Function<ApiResult<Void>, ObservableSource<ApiResult<FTaskCostDetailEntity>>>() {
            @Override
            public ObservableSource<ApiResult<FTaskCostDetailEntity>> apply(@NonNull ApiResult<Void> voidApiResult) throws Exception {
                return costDetailData;
            }
        }).compose(RxSchedulers.observableIoToMain()).compose(RxHelper.handleResult())
                .subscribeWith(new MiSubScriber<ApiResult<FTaskCostDetailEntity>>() {

                    @Override
                    protected void doOnStart() {
                        mView.showDialogLoading("");
                    }

                    @Override
                    protected void doOnNext(ApiResult<FTaskCostDetailEntity> result) {
                        if (result.data != null) {
                            mView.returnCostDetailData(result.data);
                            ToastUtils.showLong("保存成功");
                        }
                    }

                    @Override
                    protected void doOnComplete() {
                        mView.hideDialogLoading();
                    }

                    @Override
                    protected void doOnError(ApiException e) {
                        super.doOnError(e);
                        mView.hideDialogLoading();
                    }
                }));
    }
}
